package com.slj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slj.comm.responce.R;
import com.slj.model.dto.EmDTO;
import com.slj.model.entity.Employees;
import com.slj.service.EmployeesService;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


/**
 * <p>
 * 员工登录表 前端控制器
 * </p>
 *
 * @author 礼俊
 * @since 2025-09-20 22:46
 */
@RestController
@RequestMapping("/employees")
@Slf4j
public class EmployeesController {

    @Autowired
    private EmployeesService employeesService;

    @PostMapping("/login")
    @Parameters(
            value = {
                    @Parameter(name = "loginName", description = "用户名"),
                    @Parameter(name = "password", description = "密码")
            }
    )
    public R<Employees> login
            (@RequestParam String loginName, @RequestParam String password){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("login_name", loginName);
        queryWrapper.eq("password", password);
        Employees employees = employeesService.getOne(queryWrapper);
        return R.success(employees);

    }

    //分页查询员工
    @PostMapping("/pageList")
    @Parameters(
            value = {
                    @Parameter(name = "pageNum", description = "当前页"),
                    @Parameter(name = "pageSize", description = "每页数量"),
                    @Parameter(name = "empName", description = "员工姓名"),
                    @Parameter(name = "phoneNumber", description = "员工编号")
            }
    )
    public R<Page<Employees>> page(@RequestBody EmDTO emDTO){
        log.info("分页查询员工");
        System.out.println(emDTO);
        return R.success(employeesService.pageList(emDTO));
    }

    //删除员工
    @PostMapping("/delete")
    public boolean delete(@RequestParam Integer id){
        return employeesService.removeById(id);
    }


    //编辑员工
    @PostMapping("/edit")
    public R editEmloyees(@RequestBody Employees employees){
        return (R)employeesService.editEmloyees(employees);


    }
    //根据id查询员工
    @GetMapping("/getById/{id}")
    public R<Employees> getById(@PathVariable Integer id){
        return R.success(employeesService.getById(id));
    }

    //重置密码
    @GetMapping("/resetPassword/{id}")
    public R resetPassword(@PathVariable Integer id){
        Employees employees = new Employees();
        employees.setEmployeeId(id);
        employees.setPassword("123456");
 return R.success(employeesService.updateById(employees));
    }
}
